define(['template/userConfig/indexTpl','wx','wx.ajax','wx.tpl','wx.pop','vendor/bootstrap-typeahead'],function(tpl,wx){
    var userConfig = {};
	var windowId;
    function init(id){
    	windowId = wx.stringToJson(id).windowId;
    	$('<link href="/res/css/userConfig.css?t='+(new Date()|0)+'" rel="stylesheet" type="text/css" />').appendTo('head').load(function(){
    		$('#window-frame-userConfig').html(tpl.container);
            $('#Js-userConfig-cate').delegate('li','click',function(){
                var index = $(this).data('id');
                if(index == 'user'){
                    $('#Js-userConfig-body').html(tpl.search);
                    userAction();
                } else if(index == 'dep'){
                    $('#Js-userConfig-body').html(tpl.search);
                } else if(index == 'auth'){
                    $('#Js-userConfig-body').html(tpl.search);
                }
            });
            $('#Js-userConfig-cate').find('[data-id="user"]').trigger('click');
    	});
	}

    function userAction(){
        $('#Js-who-search').typeahead({
         items:10,
         ajax: {
             url:'/who/search',
             triggerLength: 1,
         },
         grepper:function(data){
             return data;
         },

         itemSelected:function(item,id){
            wx.sendData('/userconfig/user','id='+id,function(data){
                $('.who-content,.who-photo,.userConfig-user-btn').remove();
                $('#Js-userConfig-body').append(wx.tpl(tpl.detail,data.data),wx.tpl(tpl.userbtn,{id:data.data.id,code:data.data.code,hasUser:data.hasUser}));
            });
         }
        });
        $('#Js-userConfig-body').delegate('.Js-userConfig-user-add','click',function(){
            var id = $(this).parent().data('id');
            var $this = $(this);
            wx.sendData('/userconfig/adduser','id='+id,function(data){
                $this.hide().next().removeClass('hidden').show();
            });
        });
        $('#Js-userConfig-body').delegate('.Js-userConfig-user-del','click',function(){
            var id = $(this).parent().data('code');
            var $this = $(this);
            wx.sendData('/userconfig/deluser','id='+id,function(data){
                $this.hide().prev().removeClass('hidden').show();
            });
        });
    }
    userConfig.init = init;
    return userConfig;
});